
const globalData = new Map()

const storage = {
  // 获取会话内存储的值
  getSessionStorage(key = '') {
    try {
      return globalData.get(key)
    } catch (err) {
      return null
    }
  },
  // 写入会话内缓存
  setSessionStorage(key, value) {
    let tmpData = { data: value }
    try {
      globalData.set(key, tmpData.data)
      return true
    } catch (err) {
      return false
    }
  },
  // 移除会话内的单个存储
  removeSessionStorage(key) {
    try {
      return globalData.delete(key)
    } catch (err) {
      return null
    }
  },
  //本地永久存储写入有效期，有效期内会取出来，失效后会清空
  setLocalStorageSync(key, value, exp = 31536000000) {
    let tmpData = { data: value, exp, startTime: new Date().getTime() }
    uni.setStorageSync(key, tmpData)
  },
  //本地永久存储写入有效期，有效期内会取出来，失效后会清空
  setLocalStorage(key, value, exp = 31536000000) {
    let tmpData = { data: value, exp, startTime: new Date().getTime() }
    uni.setStorage({ key, data: tmpData })
  },
  // 处理数据本地数据缓存返回
  handleLocalDataBack(tmpData, key) {
    let returnData = null
    let date = new Date().getTime();
    // 如果有设置过期时间
    if (tmpData && tmpData.exp) {
      if (date - tmpData.startTime > tmpData.exp) {
        //缓存过期，清除缓存，返回false
        uni.removeStorageSync(key);
        returnData = null
      } else {
        //缓存未过期，返回值
        returnData = tmpData.data;
      }
    } else {
      returnData = tmpData
    }
    return returnData
  },
  // 移除所有的wx-mini过期的本地缓存
  removeAllWxMiniExp() {
    let that = this;
    uni.getStorageInfo({
      success: function (res) {
        let keys = res.keys
        keys.forEach(key => {
          that.handleLocalDataBack(uni.getStorageSync(key), key)
        })
      }
    })
  },
  // 移除所有的本地存储
  removeAllLocal() {
    uni.clearStorageSync()
  },
  // 移除本地所有的过期的缓存
  removeAllLocalExp() {
    this.removeAllWxMiniExp()
  },
  //本地取出存储的值
  getLocalStorage(key) {
    let tmpData = null
    tmpData = uni.getStorageSync(key) ? uni.getStorageSync(key) : null
    return this.handleLocalDataBack(tmpData, key)
  },
  //本地删除存储
  removeLocalStorage(key) {
    uni.removeStorageSync(key)
  }
}

export default storage
